const { body, query } = require("express-validator");
const { id, pageno, pagesize } = require("../common");

const title = (optional = false) => {
    if (optional) {
        return body("title")
            .optional()
            .trim()
            .notEmpty()
            .withMessage("标题不能为空")
            .bail()
            .isLength({ min: 5, max: 20 })
            .withMessage("标题长度在5-20个字符之间");
    } else {
        return body("title")
            .trim()
            .notEmpty()
            .withMessage("标题不能为空")
            .bail()
            .isLength({ min: 5, max: 20 })
            .withMessage("标题长度在5-20个字符之间");
    }
};

const content = (optional = false) => {
    if (optional) {
        return body("content")
            .optional()
            .trim()
            .notEmpty()
            .withMessage("内容不能为空")
            .bail()
            .isLength({ min: 20 })
            .withMessage("内容长度不能少于20个字符");
    } else {
        return body("content")
            .trim()
            .notEmpty()
            .withMessage("内容不能为空")
            .bail()
            .isLength({ min: 20 })
            .withMessage("内容长度不能少于20个字符");
    }
};

const isActive = body("isActive")
    .optional()
    .trim()
    .notEmpty()
    .withMessage("是否启用不能为空")
    .bail()
    .isBoolean()
    .withMessage("是否启用只能是布尔值");

module.exports = {
    list: [pageno, pagesize],
    add: [title(), content()],
    update: [id, title(true), content(true), isActive],
    delete: [id],
    detail: [id],
    status: [id]
};
